package com.example.wangziqiao.myapplication.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.wangziqiao.myapplication.R;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by wangziqiao on 2016/4/8.
 */
public class GrouponItemAdapter extends RecyclerView.Adapter<GrouponItemAdapter.MyViewHolder>{

    private JSONObject data;//待加载的数据

    public GrouponItemAdapter(JSONObject data){
        this.data = data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.groupon_item,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        try {
            JSONObject g = data.getJSONArray("rows").getJSONObject(position);
            holder.tvShortName.setText(g.getString("shortName"));
            holder.tvPrice.setText("团购价："+g.getString("price"));
            StringBuilder sb = new StringBuilder();
            sb.append("促销方式：");
            if(g.getInt("freeType")==5){
                sb.append("返现");
            }else if(g.getInt("freeType")==1){
                sb.append("免单");
            }
            holder.tvPromInfo.setText(sb.toString());
            holder.tvTimeScope.setText(g.getString("startTime")+"-"+g.getString("endTime"));
            holder.tvStatus.setText("审核通过");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        int count = 0;
        try {
            count = data.getJSONArray("rows").length();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return count;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView tvShortName;
        public TextView tvPrice;
        public TextView tvPromInfo;
        public TextView tvTimeScope;
        public TextView tvStatus;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvShortName = (TextView) itemView.findViewById(R.id.tvShortName);
            tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
            tvPromInfo = (TextView) itemView.findViewById(R.id.tvPromInfo);
            tvTimeScope = (TextView) itemView.findViewById(R.id.tvTimeScope);
            tvStatus = (TextView) itemView.findViewById(R.id.tvStatus);
        }
    }
}
